import dd from 'dingtalk-jsapi';
import env from 'dingtalk-javascript-env';

export function setTitle (title) {
  if (!title) return;
  const ua = navigator.userAgent.toLowerCase();
  const isWX = ua.match(/MicroMessenger/i) === 'micromessenger';
  const isDD = env.isDingTalk;
  if (isDD) {
    dd.ready(function () {
      dd.biz.navigation.setTitle({
        title, // 控制标题文本，空字符串表示显示默认文本
        onSuccess: function (result) {
        },
        onFail: function (err) {
          console.log(err);
        }
      });
    });
  } else if (isWX) {
    const $body = document.querySelector('body');
    document.title = title;
    const $iframe = document.createElement('iframe');
    $iframe.style.display = 'none';
    $body.appendChild($iframe);
    $iframe.onload = () => {
      setTimeout(() => {
        $iframe.parentNode.removeChild($iframe);
      });
    };
  } else {
    document.title = title;
  }
}
